package com.bookocean.infrastructure.persistence.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookocean.infrastructure.persistence.po.UserInfoPO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;

/**
 * 用户 Mapper 接口
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfoPO> {
    
    /**
     * 插入用户协议同意记录
     */
    @Insert("INSERT INTO user_agreement_records (user_id, agreement_type, agreement_version, agreed_at, user_agent, app_version, created_at) " +
            "VALUES (#{userId}, #{agreementType}, #{agreementVersion}, #{agreedAt}, #{userAgent}, #{appVersion}, NOW())")
    int insertAgreementRecord(@Param("userId") Long userId,
                             @Param("agreementType") String agreementType,
                             @Param("agreementVersion") String agreementVersion,
                             @Param("agreedAt") LocalDateTime agreedAt,
                             @Param("userAgent") String userAgent,
                             @Param("appVersion") String appVersion);
    
    /**
     * 统计用户协议同意记录数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM user_agreement_records " +
            "WHERE user_id = #{userId} AND agreement_type = #{agreementType} " +
            "<if test='agreementVersion != null and agreementVersion != \"\"'>" +
            "AND agreement_version = #{agreementVersion}" +
            "</if>" +
            "</script>")
    int countAgreementRecords(@Param("userId") Long userId,
                             @Param("agreementType") String agreementType,
                             @Param("agreementVersion") String agreementVersion);
}
